{% extends "base.html" %}

{% block title %}Home{% endblock %}

{% block content %}
<div class="row mb-4 fade-in">
    <div class="col">
        <h1 class="display-4 fw-bold text-primary mb-0">PDF Reports</h1>
        <p class="text-secondary mb-0">Browse and manage your PDF documents</p>
    </div>
    {% if current_user.is_admin %}
    <div class="col-auto d-flex align-items-center">
        <a href="{{ url_for('main.upload') }}" class="btn btn-primary">
            <i class="fas fa-upload me-2"></i>Upload New PDF
        </a>
    </div>
    {% endif %}
</div>

{% if files.items %}
<div class="card fade-in">
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th style="width: 40%">File</th>
                        <th style="width: 15%">Size</th>
                        <th style="width: 25%">Tags</th>
                        <th style="width: 20%">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    {% for file in files.items %}
                    <tr>
                        <td>
                            <div class="file-info">
                                <span class="file-name">{{ file.original_filename }}</span>
                                <small class="text-muted d-block">
                                    上传时间：{{ file.upload_date.strftime('%Y-%m-%d %H:%M:%S') }}
                                </small>
                            </div>
                        </td>
                        <td>{{ '{:.1f}'.format(file.file_size / 1024) }} KB</td>
                        <td>
                            {% if file.tags %}
                            {% for tag in file.tags.split(',') %}
                            <span class="badge bg-light text-dark me-1">{{ tag.strip() }}</span>
                            {% endfor %}
                            {% endif %}
                        </td>
                        <td>
                            <div class="btn-group">
                                <a href="{{ url_for('main.view_file', filename=file.filename) }}" class="btn btn-primary btn-sm" title="查看文件">
                                    <i class="fas fa-eye"></i> 查看
                                </a>
                                <a href="{{ url_for('main.download_file', filename=file.filename) }}" class="btn btn-outline-primary btn-sm" title="下载文件">
                                    <i class="fas fa-download"></i> 下载
                                </a>
                                {% if current_user.is_admin %}
                                <a href="{{ url_for('main.delete_file', file_id=file.id) }}" class="btn btn-danger btn-sm" onclick="return confirm('确定要删除这个文件吗？')" title="删除文件">
                                    <i class="fas fa-trash"></i>
                                </a>
                                {% endif %}
                            </div>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>

<nav aria-label="Page navigation" class="mt-4">
    <ul class="pagination justify-content-center">
        {% if files.has_prev %}
        <li class="page-item">
            <a class="page-link" href="{{ url_for('main.index', page=files.prev_num) }}">
                <i class="fas fa-chevron-left"></i>
            </a>
        </li>
        {% else %}
        <li class="page-item disabled">
            <span class="page-link">
                <i class="fas fa-chevron-left"></i>
            </span>
        </li>
        {% endif %}

        {% for page_num in files.iter_pages(left_edge=2, left_current=2, right_current=3, right_edge=2) %}
        {% if page_num %}
        <li class="page-item {% if page_num == files.page %}active{% endif %}">
            <a class="page-link" href="{{ url_for('main.index', page=page_num) }}">{{ page_num }}</a>
        </li>
        {% else %}
        <li class="page-item disabled">
            <span class="page-link">...</span>
        </li>
        {% endif %}
        {% endfor %}

        {% if files.has_next %}
        <li class="page-item">
            <a class="page-link" href="{{ url_for('main.index', page=files.next_num) }}">
                <i class="fas fa-chevron-right"></i>
            </a>
        </li>
        {% else %}
        <li class="page-item disabled">
            <span class="page-link">
                <i class="fas fa-chevron-right"></i>
            </span>
        </li>
        {% endif %}
    </ul>
</nav>
{% else %}
<div class="card fade-in">
    <div class="card-body text-center py-5">
        <i class="far fa-folder-open display-4 text-secondary mb-3"></i>
        <h3 class="text-secondary">暂无PDF文件</h3>
        {% if current_user.is_admin %}
        <p class="text-muted">点击上方的"Upload New PDF"按钮上传文件</p>
        {% else %}
        <p class="text-muted">请联系管理员上传文件</p>
        {% endif %}
    </div>
</div>
{% endif %}
{% endblock %}

{% block scripts %}
<script src="https://kit.fontawesome.com/your-code.js" crossorigin="anonymous"></script>
{% endblock %}
